package com.sx.pipeline.ui.territory

import android.graphics.Color
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.amap.api.location.DPoint
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.CoordinateConverter
import com.amap.api.maps.model.*
import com.sx.mvvmlibs.utils.Logs
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYBaseAppBarActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActTerritoryDetailsBinding
import com.sx.pipeline.repos.http.res.TerritoryVO
import com.sx.pipeline.ui.map.DistrictDrawHelper
import com.sx.pipeline.ui.map.entity.DrawLatLng
import com.sx.pipeline.ui.map.tools.DrawMapUtils

/**
 * @Author: YY
 * @Date: 2022/8/5
 * @Desc: 区域详情
 */
@Route(path = RoutePath.Territory.TERRITORY_DETAILS)
class TerritoryDetailsActivity: CYBaseAppBarActivity<ActTerritoryDetailsBinding, TerritoryVM>
    (R.layout.act_territory_details) {

    private lateinit var aMap: AMap

    @Autowired(name = IntentKey.TYPE)
    @JvmField
    var type : Int? = 0

    @Autowired(name = IntentKey.DATA)
    @JvmField
    var territoryVO: TerritoryVO? = null

    // 绘制多边形
    private lateinit var polygon: Polygon

    // 绘制点列表
    private val pointList: MutableList<DrawLatLng> = mutableListOf()

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        mBinding.mapView.onSaveInstanceState(outState)
    }

    override fun initData(bundle: Bundle?) {
        super.initData(bundle)
        mBinding.mapView.onCreate(bundle)
        if (!this::aMap.isInitialized) {
            aMap = mBinding.mapView.map
        }

        if (territoryVO != null) {
            getTitleBar().title = territoryVO!!.name
            territoryVO!!.centerList.forEach {
                val latLng = LatLng(it.latitude.toDouble(), it.longitude.toDouble())
                var drawLatLng = DrawLatLng(latLng, false)
                if (territoryVO?.type != 0) {
                    drawLatLng = DrawLatLng(convert(latLng)!!, false)
                }
                pointList.add(drawLatLng)
            }
            drawPolygon()
        }
        aMap.moveCamera(CameraUpdateFactory.newCameraPosition(CameraPosition(pointList[0].latLng, 14f, 0f, 0f)))
    }

    override fun onResume() {
        mBinding.mapView.onResume()
        super.onResume()
    }

    override fun onPause() {
        mBinding.mapView.onPause()
        super.onPause()
    }

    override fun onDestroy() {
        mBinding.mapView.onDestroy()
        pointList.clear()
        super.onDestroy()
    }


    /**
     * 根据类型 转换 坐标
     */
    fun convert(sourceLatLng: LatLng?): LatLng? {
        val converter = CoordinateConverter(this)
        // CoordType.GPS 待转换坐标类型
        converter.from(CoordinateConverter.CoordType.GPS)
        // sourceLatLng待转换坐标点
        converter.coord(sourceLatLng)
        // 执行转换操作
        return converter.convert()
    }

    /**
     * 绘制多边形
     */
    private fun drawPolygon() {
        if (pointList.size <= 2) {
            return
        }
        if (this::polygon.isInitialized) {
            polygon.remove()
        }

        // 多边形参数对象
        val polygonOptions = PolygonOptions()
        // 添加 多边形的每个顶点（顺序添加）
        for (i in pointList.indices) {
            polygonOptions.add(pointList[i].latLng)
            Logs.d("drawPolygon ${pointList[i].latLng}")
        }
        polygonOptions.strokeWidth(2f)
            .strokeColor(Color.parseColor("#ffffff")) // 边框颜色
            .fillColor(Color.parseColor("#66000000")) // 多边形的填充色
        Logs.d("drawPolygon ")
        polygon = this.aMap.addPolygon(polygonOptions.zIndex(100f))
    }
}